package com.jsgt.greekfire.ui.main.invite

import android.Manifest
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.pm.PackageManager
import android.graphics.Bitmap
import android.os.Build
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import com.fhx.core.base.BaseActivity
import com.fhx.core.utils.QRCodeUtils
import com.fhx.core.utils.ToastUtils
import com.jsgt.greekfire.App
import com.jsgt.greekfire.R
import com.jsgt.greekfire.common.util.ViewCapture
import kotlinx.android.synthetic.main.activity_invite.*
import org.jetbrains.anko.downloadManager

class InviteActivity : BaseActivity(), View.OnClickListener {
    val REQUEST_CODE_ASK_PERMISSIONS = 100
    private var posterAdapter: InvitePosterAdapter? = null
    private var posterList: MutableList<InvitePoster>? = null
    private var appDownloadUrl: String? = null

    override fun needFitsSystemWindows(): Boolean {
        return false
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_invite
    }

    override fun main() {
        setStatusBar(R.color.transparent, true, R.color.black)
        setTitle(R.string.invite_title)
        appDownloadUrl = App.context.getMemCache("appDownloadUrl").toString()
        if (appDownloadUrl.isNullOrEmpty()) {
            finish()
        } else {
            posterList = mutableListOf(
                InvitePoster(R.mipmap.poster1, appDownloadUrl!!),
                InvitePoster(
                    R.mipmap.poster2,
                    appDownloadUrl!!
                ),
                InvitePoster(
                    R.mipmap.poster3,
                    appDownloadUrl!!
                ),
                InvitePoster(R.mipmap.poster4, appDownloadUrl!!)
            )
            posterAdapter = InvitePosterAdapter()
            val qrBitmap: Bitmap ?=  QRCodeUtils.createQRCodeBitmap(appDownloadUrl, 400, 400)
            posterAdapter!!.setQRBitmap(qrBitmap)

            recycler.layoutManager = GridLayoutManager(this, 2)
            recycler.adapter = posterAdapter
            posterAdapter!!.setNewInstance(posterList)
            llSave.setOnClickListener(this)
            llLink.setOnClickListener(this)
        }
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.llSave -> {
                llSave.isEnabled = false
                if (!checkHasStoragePermission()) {
                    llSave.isEnabled = true
                    return
                }
                val bitmap: Bitmap? =
                    ViewCapture.getViewBitmap(posterAdapter?.posterView)
                val b: Boolean = ViewCapture.saveImageToGallery(this, bitmap)
                ToastUtils.show(if (b) R.string.invite_save_success else R.string.invite_save_success)
                llSave.isEnabled = true
            }
            R.id.llLink -> {
                val cm =
                    getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
                cm.setPrimaryClip(
                    ClipData.newPlainText(
                        "text",
                        appDownloadUrl
                    )
                )
                ToastUtils.show(R.string.invite_copy_success)
            }
        }
    }


    private fun checkHasStoragePermission(): Boolean {
        // 从6.0系统(API 23)开始，访问外置存储需要动态申请权限
        if (Build.VERSION.SDK_INT >= 23) {
            val checkPermission =
                checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
            if (checkPermission != PackageManager.PERMISSION_GRANTED) {
                requestPermissions(
                    arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),
                    REQUEST_CODE_ASK_PERMISSIONS
                )
                return false
            }
        }
        return true
    }
}